#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using VisualDB.Entities;
using System.Diagnostics;
using VisualDB.DAL;

#endregion

namespace VisualDB
{
    public partial class RegisterDatabaseForm : Form
    {
        private IDatabaseDAL _databaseDAL;
        private DatabaseEntity _databaseEntity;

        public RegisterDatabaseForm()
        {
            InitializeComponent();
        }

        private void frmConnectDatabase_Load(object sender, EventArgs e)
        {
            this.txtServerName.Focus();
            FillDatabaseTypes();
            WindowsAuthentication();
        }

        public IDatabaseDAL DatabaseDAL
        {
            get
            {
                return _databaseDAL;
            }
        }

        private void FillDatabaseTypes()
        {
            this.cboDatabaseType.DataSource = DatabaseType.GetDatabaseTypes();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DatabaseEntity database = (DatabaseEntity)this.cboDatabase.SelectedItem;
            this._databaseEntity = database;
            this._databaseDAL.DatabaseEntity.DatabaseName = database.DatabaseName;
            
            this._databaseDAL.Open();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void rdbWindowsAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            WindowsAuthentication();
        }

        private void WindowsAuthentication()
        {
            this.txtUsername.Enabled = false;
            this.txtPassword.Enabled = false;
        }

        private void rdbSQLServerAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            SQLServerAuthentication();
        }

        private void SQLServerAuthentication()
        {
            this.txtUsername.Enabled = true;
            this.txtPassword.Enabled = true;
        }

        private void btnRefreshDatabases_Click(object sender, EventArgs e)
        {
            DatabaseEntity database = new DatabaseEntity();
            database.DatabaseType = (DatabaseType)this.cboDatabaseType.SelectedItem;
            database.UseWindowsAuthentication = this.rdbWindowsAuthentication.Checked;
            database.ServerName  =this.txtServerName.Text;
            database.Username = this.txtUsername.Text;
            database.Password = this.txtPassword.Text;

            this._databaseDAL = DALFactory.GetDatabaseDAL(database);

            string errors;

            List<DatabaseEntity> databases = this._databaseDAL.GetDatabases(out errors);

            if (string.IsNullOrEmpty(errors))
                this.cboDatabase.DataSource = databases;
            else
                MessageBox.Show(errors, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void cboDatabaseType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtServerName.Text = "";
            this.txtUsername.Text = "";
            this.txtPassword.Text = "";
            this.cboDatabase.Items.Clear();

            if (this.cboDatabaseType.SelectedItem != null)
            {
                DatabaseType databaseType = (DatabaseType)this.cboDatabaseType.SelectedItem;

                switch (databaseType.Name)
                {
                    case DatabaseType.MySQL:
                        this.rdbSQLServerAuthentication.Visible = false;
                        this.rdbWindowsAuthentication.Visible = false;
                        SQLServerAuthentication();
                        break;

                    case DatabaseType.SQLServer:
                        this.rdbSQLServerAuthentication.Visible = true;
                        this.rdbWindowsAuthentication.Visible = true;
                        WindowsAuthentication();
                        break;
                }
            }
        }
    }
}